<?php

/**
 * 后台友情链接模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: user.class.php 89 2011-12-04 13:39:27Z zwt007 $
 * http://www.t-mac.org；
 */
class user extends Model
{

    private $db;
    private $user_table;
    private $userstype_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->user_table = DB_PREFIX . 'users';
        $this->userstype_table = DB_PREFIX . 'users_type';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addUser($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->user_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个管理员信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getuserInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->user_table WHERE uid = $id LIMIT 0,1");
        return $rs;
    }

    public function checkUserName($username, $id)
    {
        $sql = "SELECT * FROM $this->user_table WHERE username = '{$username}'";
        if ($id <> 0)
            $sql .= " AND uid <> $id";
        $sql .= " LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        if ($rs) {
            $this->redirect('管理员名称重复啦!');
            exit;
        }
    }

    /**
     * 获取所有管理员
     * return article_class,pages
     */
    public function getUserList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->user_table");
        if ($url == '')
            $url = PHP_SELF . '?m=user&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(10);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT * FROM $this->user_table ORDER BY uid ASC LIMIT $limit");

        //取管理员类型option数组
        $admintype_ary = $this->getAdminType();
        $admintype_array = array();
        foreach ($admintype_ary AS $kk => $vv) {
            $admintype_array[$vv['rank']] = $vv['type_name'];
        }

        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['typename'] = $admintype_array[$v['rank']];
                $rs[$k]['time'] = date('Y-m-d H:i:s', $v['reg_time']);
            }
        }
        //把文章的当前page写到cookies里
        HttpResponse::setCookie('user_page', $pages->getNowPage());

        if ($count == 0) {
            $ErrorMsg = "暂无管理员!   <a href='" . PHP_SELF . "?m=user.add' class='link_a'>点我来添加新友情链接</a>";
        } else {
            $ErrorMsg = '';
        }

        $result = array('rs' => $rs, 'pageCurrent' => $pages->getNowPage(), 'page' => $pages->show(), 'ErrorMsg' => $ErrorMsg);
        return $result;
    }

    /**
     * 取管理员类别
     */
    public function getAdminType()
    {
        $rs = $this->db->cacheGetAll("SELECT * FROM $this->userstype_table ORDER BY rank ASC", 86400);
        return $rs;
    }

    /**
     * del
     * @param int $id
     */
    public function delUser($id)
    {
        $rs = $this->db->execute("DELETE FROM $this->user_table WHERE uid in ($id)");
        return $rs;
    }

}